﻿using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Mossquito.Moss.SharePoint.CommonControls.Utils
{
    /// <summary>
    /// Helper class for creating properties in editor helper
    /// </summary>
	public static class EditorPartHelper
    {
		/// <summary>
		/// Creates a property control of given type and adds it to parent control's Controls collection
		/// </summary>
		/// <typeparam name="TControl">Type of property</typeparam>
		/// <param name="controlParent">Parent control</param>
		/// <param name="id">ID of the property control</param>
		/// <param name="headerText">Header text of the control</param>
		/// <returns>Returns created control</returns>
		public static TControl AddPropertyControl<TControl>(Control controlParent, string id, string headerText) where TControl : Control, new()
		{
			TControl control;
			if (typeof(TControl) == typeof(CheckBox))
			{
				control = AddPropertyCheckBox(controlParent, id, headerText) as TControl;
			}
			else if (typeof(TControl) == typeof(CheckBoxList))
			{
				control = AddPropertyCheckBoxList(controlParent, id, headerText) as TControl;
			}
			else if (typeof(TControl) == typeof(Button))
			{
				control = AddButton(controlParent, id, headerText) as TControl;
			}
			else
			{
				// add label
				string labelHeaderID = "labelHeader" + id;
				AddLabel(controlParent, labelHeaderID, headerText);

				// add control
				control = new TControl
				{
					ID = id
				};
				controlParent.Controls.Add(control);
			}
			
			return control;
		}

		/// <summary>
		/// Adds <see cref="CheckBox"/> property control
		/// </summary>
		/// <param name="controlParent">Parent control</param>
		/// <param name="id">ID of the property control</param>
		/// <param name="text">Text of the <see cref="CheckBox"/></param>
		/// <returns>Returns created control</returns>
    	private static CheckBox AddPropertyCheckBox(Control controlParent, string id, string text)
    	{
    		CheckBox checkBox = new CheckBox
    		                    	{
    		                    		ID = id,
    		                    		Text = text
    		                    	};
    		HtmlGenericControl div = new HtmlGenericControl("div");
    		div.Controls.Add(checkBox);

    		controlParent.Controls.Add(div);
			
    		return checkBox;
    	}

		/// <summary>
		/// Adds <see cref="CheckBoxList"/> property control
		/// </summary>
		/// <param name="controlParent">Parent control</param>
		/// <param name="id">ID of the property control</param>
		/// <param name="headerText">Header text of the control</param>
		/// <returns>Returns created control</returns>
    	private static CheckBoxList AddPropertyCheckBoxList(Control controlParent, string id, string headerText)
    	{
    		string labelHeaderID = "labelHeader" + id;
    		AddLabel(controlParent, labelHeaderID, headerText);
			
    		CheckBoxList checkBoxList = new CheckBoxList
    		                            	{
    		                            		ID = id
    		                            	};
    		checkBoxList.Style["text-align"] = "left";
			
    		HtmlGenericControl div = new HtmlGenericControl("div");
    		div.Controls.Add(checkBoxList);
			
    		controlParent.Controls.Add(div);

    		return checkBoxList;
    	}

		/// <summary>
		/// Adds <see cref="Label"/> property control
		/// </summary>
		/// <param name="controlParent">Parent control</param>
		/// <param name="id">ID of the property control</param>
		/// <param name="text">Text of the <see cref="Label"/></param>
		/// <returns>Returns created control</returns>
    	private static void AddLabel(Control controlParent, string id, string text)
    	{
    		Label label = new Label
    		              	{
    		              		ID = id,
    		              		Text = text,
    		              		Width = new Unit(100, UnitType.Percentage)
    		              	};

    		controlParent.Controls.Add(label);
    	}

		/// <summary>
		/// Adds <see cref="Button"/> property control
		/// </summary>
		/// <param name="controlParent">Parent control</param>
		/// <param name="id">ID of the property control</param>
		/// <param name="text">Text of the <see cref="Button"/></param>
		/// <returns>Returns created control</returns>
    	private static Button AddButton(Control controlParent, string id, string text)
    	{
    		Button button = new Button
    		                	{
    		                		ID = id,
    		                		Text = text
    		                	};

    		controlParent.Controls.Add(button);

    		return button;
    	}

    }
}
